package com.manor.main.fragment.user

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.lxj.xpopup.XPopup
import com.manor.common.Constants
import com.manor.common.bean.UserNBean
import com.manor.common.bean.UserNBean.*
import com.manor.common.fragment.BaseFragment
import com.manor.common.interfaces.CommonCallback
import com.manor.common.utils.StringUtil
import com.manor.main.R
import com.manor.main.adapter.*
import com.manor.main.bean.UserFlowBean
import com.manor.main.dialog.MineMedalPopup
import com.manor.main.http.MainHttpConsts
import com.manor.main.http.MainHttpUtil
import com.manor.main.utils.MGridManager
import kotlinx.android.synthetic.main.fragment_user_basic.*
import java.util.*

/**
 * 我的基本信息
 *
 *
 * @date  2020/7/14  18:05
 * @version 1.0
 */
class UserBasicFragment : BaseFragment() {
    private var mUid = ""
    private val jzDataList: MutableList<MedalBean> = ArrayList()
    private var jzAdapter: JzAdapter? = null
    private val giftDataList: MutableList<GiftlistBean.ListBean> = ArrayList()
    private var giftAdapter: GiftAdapter? = null
    private val evaDataList: MutableList<GoodBean> = ArrayList()
    private var evaluateAdapter: EvaluateAdapter? = null
    private var mBasicAdapter: MBasicAdapter? = null
    private var mHobbyAdapter: MHobbyAdapter? = null
    private val basicList: MutableList<UserFlowBean> = ArrayList()
    private val hobbyList: MutableList<String> = ArrayList()


    companion object {
        fun newInstance(uId: String): UserBasicFragment {
            return UserBasicFragment().apply {
                arguments = Bundle().apply {
                    putString(Constants.TO_UID, uId)
                }
            }
        }
    }

    override fun getLayoutRes(): Int = R.layout.fragment_user_basic

    override fun initView() {
        mUid = arguments?.getString(Constants.TO_UID) ?: ""
//        initRecycler()
    }

    override fun onResume() {
        super.onResume()
        doNet()
    }

    override fun onStop() {
        super.onStop()
        MainHttpUtil.cancel(MainHttpConsts.GET_BASE_INFO)
    }

    private fun initRecycler() {
        rvJzRecycler.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL,
                false)
        jzAdapter = JzAdapter(R.layout.item_sd_jz, jzDataList)
        rvJzRecycler.adapter = jzAdapter
        jzAdapter?.setOnItemClickListener { adapter, view, position ->
            showMedal(jzDataList)
        }

        rvGift.layoutManager = MGridManager(context, 4)
        giftAdapter = GiftAdapter(R.layout.item_user_gift, giftDataList)
        rvGift.adapter = giftAdapter

        evaluateAdapter = EvaluateAdapter(R.layout.item_user_evaluate, evaDataList)
        rvEvaluate.layoutManager = MGridManager(context, 4)
        rvEvaluate.adapter = evaluateAdapter

        mBasicAdapter = MBasicAdapter(basicList)
        aflBasic.adapter = mBasicAdapter

        mHobbyAdapter = MHobbyAdapter(hobbyList)
        aflHobby.adapter = mHobbyAdapter
    }

    /**
     * 展示奖章
     */
    private fun showMedal(dataList: MutableList<MedalBean>) {
        if (StringUtil.isListNotEmpty(dataList))
            XPopup.Builder(requireActivity())
                    .asCustom(MineMedalPopup(requireActivity(), dataList))
                    .show()
    }

    private fun doNet() {
        MainHttpUtil.getBaseInfo(mUid, object : CommonCallback<UserNBean>() {
            override fun callback(bean: UserNBean?) {
                //bean?.run {
                bean?.basics?.run {
                    tv_right_city?.text = "${this.province}${this.city}${this.district}".ifEmpty { "无" }

                    tv_right_job?.text = this.occupation.ifEmpty { "无" }
                    tv_right_cell?.text = this.user_login.ifEmpty { "无" }

//                    tvJzNum?.text = "已获得" + basics.count + "个"
//                    jzDataList.clear()
//                    jzDataList.addAll(this.medal)
//                    jzAdapter?.notifyDataSetChanged()
//
//                    evaDataList.clear()
//                    if (StringUtil.isListNotEmpty(this.evaluate?.good)) {
//                        for (goodBean in this.evaluate.good) {
//                            goodBean.type = 1
//                            goodBean.sex = basics.sex
//                            evaDataList.add(goodBean)
//                        }
//                    }
//                    for (badBean in this.evaluate.bad) {
//                        badBean?.sex = basics.sex
//                        evaDataList.add(badBean)
//                    }
//                    if (evaDataList.size <= 0) {
//                        evaDataList.add(GoodBean())
//                    }
//                    evaluateAdapter?.notifyDataSetChanged()
//
//                    giftDataList.clear()
//                    giftDataList.addAll(this.giftlist.list)
//                    if (giftDataList.size <= 0) {
//                        giftDataList.add(GiftlistBean.ListBean())
//                    }
//                    giftAdapter?.notifyDataSetChanged()
//
//                    basicList.clear()
//                    if (StringUtil.isNotEmpty(basics?.age)) {
//                        basicList.add(UserFlowBean(basics.age, this.basics.sex))
//                    }
//                    if (StringUtil.isNotEmpty(this.basics?.city)) {
//                        basicList.add(UserFlowBean(this.basics.city))
//                    }
//                    if (StringUtil.isNotEmpty(this.basics?.height)) {
//                        basicList.add(UserFlowBean(this.basics.height))
//                    }
//                    if (StringUtil.isNotEmpty(this.basics?.constellation)) {
//                        basicList.add(UserFlowBean(this.basics.constellation))
//                    }
//                    if (StringUtil.isNotEmpty(this.basics?.occupation)) {
//                        basicList.add(UserFlowBean(this.basics.occupation))
//                    }
//                    if (StringUtil.isNotEmpty(this.basics?.education)) {
//                        basicList.add(UserFlowBean(this.basics.education))
//                    }
//
//                    if (StringUtil.isNotEmpty(this.basics?.marriage)) {
//                        basicList.add(UserFlowBean(this.basics.marriage))
//                    }
//                    if (StringUtil.isNotEmpty(this.basics?.garage)) {
//                        basicList.add(UserFlowBean(this.basics.garage))
//                    }
//
//                    if (basicList.size <= 0) {
//                        basicList.add(UserFlowBean("暂无"))
//                    }
//                    mBasicAdapter?.notifyDataChanged()
//
//                    hobbyList.clear()
//                    if (StringUtil.isNotEmpty(this.interest?.sports)) {
//                        hobbyList.add(this.interest.sports)
//                    }
//                    if (StringUtil.isNotEmpty(this.interest?.books)) {
//                        hobbyList.add(this.interest.books)
//                    }
//                    if (StringUtil.isNotEmpty(this.interest?.food)) {
//                        hobbyList.add(this.interest.food)
//                    }
//                    if (StringUtil.isNotEmpty(bean.interest?.movie)) {
//                        hobbyList.add(this.interest.movie)
//                    }
//                    if (StringUtil.isNotEmpty(this.interest?.music)) {
//                        hobbyList.add(this.interest.music)
//                    }
//                    if (hobbyList.size <= 0) {
//                        hobbyList.add("暂无")
//                    }
//                    mHobbyAdapter?.notifyDataChanged()


                }
            }
        })
    }


}